package com.thon.controller.admin;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.thon.commons.utils.Encodes;
import com.thon.commons.utils.PropertiesLoader;
import com.thon.controller.util.BaseController;
import com.thon.dao.util.Page;
import com.thon.entity.project.Preferential;
import com.thon.entity.project.Product;
import com.thon.entity.project.Project;
import com.thon.entity.user.Charitarian;
import com.thon.entity.user.Institution;
import com.thon.entity.user.User;
import com.thon.service.project.PreferentialService;
import com.thon.service.project.ProjectService;
import com.thon.service.user.CharitarianService;
import com.thon.service.user.InstitutionService;
import com.thon.service.user.UserService;

/**
 * @file SystemController.java
 * @author Zemo
 * @email zemochen@lanbaoo.com
 * @date 2013年12月4日 上午9:40:40
 * @description 控制系统后台管理页面跳转
 */
@Controller
@RequestMapping(value = "/{admin_url}")
public class AdminController extends BaseController {
	
	private static final String ADMIN_URL=PropertiesLoader.getConfiguration("application.properties", "admin.url");	
	
	@Autowired
	private UserService userService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private InstitutionService institutionService;
	@Autowired
	private CharitarianService charitarianService;
	@Autowired
	private PreferentialService preferentialService;

	@RequestMapping(method = RequestMethod.GET)
	public String index(@PathVariable(value="admin_url") String admin_url,Model model) {
		if(checkUrl(admin_url)){			
			return "admin-index";
		}else{
			return "login";
		}
	}

	private boolean checkUrl(String admin_url) {
		try {
			String decode=IOUtils.toString(Encodes.decodeBase64(admin_url));
			String[] dStr=StringUtils.split(decode, '&');
			if(dStr[0].equals(ADMIN_URL)){
				return true;
			}else{
				return false;
			}
		} catch (IOException e) {
			return false;
		}
		
	}
	// 字典列表
		@RequestMapping(value = "/dic/list", method = RequestMethod.GET)
		public String dicList(@PathVariable(value="admin_url") String admin_url) {
			if(checkUrl(admin_url)){			
				return "dic-list";
			}else{
				return "login";
			}
			
		}

	// 用户列表
	@RequestMapping(value = "/user/list", method = RequestMethod.GET)
	public String userList(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){			
			return "admin-user-list";
		}else{
			return "login";
		}
		
	}
	// 更新用户信息
	@RequestMapping(value = "/user/update", method = RequestMethod.GET)
	public String updateUser(@PathVariable(value="admin_url") String admin_url,HttpServletRequest reuqest, Model model) {
		if(checkUrl(admin_url)){			
			int userId = Integer.parseInt(reuqest.getParameter("uid"));
			User user = new User();
			user = userService.getUser(userId);
			model.addAttribute("user", user);
			return "user-add-edit";
		}else{
			return "login";
		}
	}

	// 更新项目信息
	@RequestMapping(value = "/project/update", method = RequestMethod.GET)
	public String updateProject(@PathVariable(value="admin_url") String admin_url,HttpServletRequest reuqest, Model model) {
		if(checkUrl(admin_url)){			
			int projectId = Integer.parseInt(reuqest.getParameter("pid"));
			Project project = projectService.getProject(0, projectId);
			model.addAttribute("project", project);
			return "project-edit";
		}else{
			return "login";
		}
		
	}

	// 用户账号认证
	@RequestMapping(value = "/verify/user", method = RequestMethod.GET)
	public String verifyUser(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){						
			return "verifyUser";
		}else{
			return "login";
		}
	}

	// 更新机构
	@RequestMapping(value = "/institution/update", method = RequestMethod.GET)
	public String updateInstitution(@PathVariable(value="admin_url") String admin_url,HttpServletRequest reuqest, Model model) {
		if(checkUrl(admin_url)){			
			int userId = Integer.parseInt(reuqest.getParameter("id"));
			Institution institution = institutionService
					.getInstitutionByUser(userId);
			model.addAttribute("institution", institution);
			return "institution-edit";		
		}else{
			return "login";
		}
	}

	// 更新社会责任家
	@RequestMapping(value = "/charitarian/update", method = RequestMethod.GET)
	public String updateCharitarian(@PathVariable(value="admin_url") String admin_url,HttpServletRequest reuqest, Model model) {
		if(checkUrl(admin_url)){			
			int id = Integer.parseInt(reuqest.getParameter("id"));
			Charitarian charitarian = charitarianService.getCharitarian(id);
			model.addAttribute("charitarian", charitarian);
			return "charitarian-edit";			
		}else{
			return "login";
		}
	}

	// 特殊项目审核
	@RequestMapping(value = "/verify/project", method = RequestMethod.GET)
	public String verifyProject(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){						
			return "verifyProject";
		}else{
			return "login";
		}
	}

	// 项目管理
	@RequestMapping(value = "/manage/project", method = RequestMethod.GET)
	public String manageProject(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){			
			return "manageProject";		
		}else{
			return "login";
		}
	}

	// 项目分类管理
	@RequestMapping(value = "/manage/category", method = RequestMethod.GET)
	public String manageCategory(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){						
			return "manageCategory";
		}else{
			return "login";
		}
	}
	//项目收益分类管理
	@RequestMapping(value = "/project/benefit", method = RequestMethod.GET)
	public String projectbenefit(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){						
			return "benefit";
		}else{
			return "login";
		}
	}
	//开放平台管理
	@RequestMapping(value = "/project/open", method = RequestMethod.GET)
	public String openplatform(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){						
			return "adminopen";
		}else{
			return "login";
		}
	}
	
	//服务发起协议管理
		@RequestMapping(value = "/project/protrol", method = RequestMethod.GET)
		public String projectprotrol(@PathVariable(value="admin_url") String admin_url) {
			if(checkUrl(admin_url)){						
				return "protrol";
			}else{
				return "login";
			}
		}
	
	//优惠券使用说明管理
		@RequestMapping(value = "/coupon/desc", method = RequestMethod.GET)
		public String coupondesc(@PathVariable(value="admin_url") String admin_url) {
			if(checkUrl(admin_url)){						
				return "coupon_desc";
			}else{
				return "login";
			}
		}		
	//本网站静态文字、图片管理
		@RequestMapping(value = "/web/desc", method = RequestMethod.GET)
		public String webdesc(@PathVariable(value="admin_url") String admin_url) {
			if(checkUrl(admin_url)){						
				return "web_desc";
			}else{
				return "login";
			}
		}	
	//本网站静态文字、图片管理
		@RequestMapping(value = "/web/aboutus", method = RequestMethod.GET)
		public String aboutus(@PathVariable(value="admin_url") String admin_url) {
			if(checkUrl(admin_url)){						
				return "aboutus";
			}else{
				return "login";
			}
		}
	//优惠活动管理
		@RequestMapping(value = "/manage/preferential", method = RequestMethod.GET)
		public String managepreferential(@PathVariable(value="admin_url") String admin_url) {
			if(checkUrl(admin_url)){						
				return "preferential";
			}else{
				return "login";
			}
		}
	
		//优惠活动列表
		@RequestMapping(value = "/manage/preferential/list", method = RequestMethod.GET)
		public String preferential_list(@PathVariable(value="admin_url") String admin_url) {
			if(checkUrl(admin_url)){						
				return "preferential_list";
			}else{
				return "login";
			}
		}
		//优惠活动管理
		@RequestMapping(value = "/manage/preferential/edit", method = RequestMethod.GET)
		public String preferential_edit(@PathVariable(value="admin_url") String admin_url,HttpServletRequest reuqest, Model model) {
			if(checkUrl(admin_url)){			
				int id = Integer.parseInt(reuqest.getParameter("pid"));
				Preferential preferential = preferentialService.getPreferential(id);
				model.addAttribute("preferential", preferential);										
				return "preferential_edit";
			}else{
				return "login";
			}
		}	
		
		
	//优惠券列表管理
	@RequestMapping(value = "/manage/coupon", method = RequestMethod.GET)
	public String managecoupon(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){						
			return "coupon";
		}else{
			return "login";
		}
	}
	//优惠券派发管理
	@RequestMapping(value = "/manage/coupon/provide", method = RequestMethod.GET)
	public String couponprovide(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){						
			return "couponprovide";
		}else{
			return "login";
		}
	}
	//优惠券使用管理
	@RequestMapping(value = "/manage/coupon/use", method = RequestMethod.GET)
	public String couponuse(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){						
			return "couponuse";
		}else{
			return "login";
		}
	}
	// 地区管理
	@RequestMapping(value = "/manage/region", method = RequestMethod.GET)
	public String manageRegion(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){						
			return "manageRegion";
		}else{
			return "login";
		}
	}

	// 积善宝管理
	@RequestMapping(value = "/manage/fund", method = RequestMethod.GET)
	public String manageFund(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){			
			
			return "manageFund";
		}else{
			return "login";
		}
	}

	// 广告管理
	@RequestMapping(value = "/manage/ads", method = RequestMethod.GET)
	public String manageAds(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){			
			
			return "manageAds";
		}else{
			return "login";
		}
	}

	// 推荐项目设置
	@RequestMapping(value = "/set/recommend", method = RequestMethod.GET)
	public String setRecommend(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){			
			
			return "setRecommend";
		}else{
			return "login";
		}
	}

	// 积善值设置
	@RequestMapping(value = "/set/fund", method = RequestMethod.GET)
	public String setFund(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){			
			
			return "setFund";
		}else{
			return "login";
		}
	}

	// 第三方KEY登录设置
	@RequestMapping(value = "/set/bind", method = RequestMethod.GET)
	public String setBind(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){			
			
			return "setBind";
		}else{
			return "login";
		}
	}
	
	// 充值管理
	@RequestMapping(value = "/manage/deposit", method = RequestMethod.GET)
	public String fundDetailList(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){						
			return "deposit";
		}else{
			return "login";
		}
	}
	
	//管理员管理
	@RequestMapping(value = "/profile", method = RequestMethod.GET)
	public String manageAdmin(@PathVariable(value="admin_url") String admin_url) {
		if(checkUrl(admin_url)){			
			
			return "profile";
		}else{
			return "login";
		}
	}
	
	//发送站内信
	@RequestMapping(value="/sendmessage",method=RequestMethod.GET)
	public String sendMessage(@PathVariable(value="admin_url") String admin_url,HttpServletRequest reuqest, Model model){
		if(checkUrl(admin_url)){			
			int userId = Integer.parseInt(reuqest.getParameter("uid"));
			User user = new User();
			user = userService.getUser(userId);
			model.addAttribute("user", user);
			return "sendMessage";
			
		}else{
			return "login";
		}
	}
	
	//查看用户财务明细
	@RequestMapping(value="/manage/finance",method=RequestMethod.GET)
	public String listFiance(@PathVariable(value="admin_url") String admin_url){
		if(checkUrl(admin_url)){			
			return "finance";
			
		}else{
			return "login";
		}
	}
	
	//查看提现申请
		@RequestMapping(value="/manage/getcash",method=RequestMethod.GET)
		public String getcash(@PathVariable(value="admin_url") String admin_url){
			if(checkUrl(admin_url)){			
				return "getcash";
				
			}else{
				return "login";
			}
		}
		//字典
		@RequestMapping(value = "/dic", method = RequestMethod.GET)
		public String dic(@PathVariable(value = "admin_url") String admin_url) {
			if (checkUrl(admin_url)) {
				return "dic";

			} else {
				return "login";
			}
		}
					
		// 字典内容
		@RequestMapping(value = "/dicInfo", method = RequestMethod.GET)
		public String dicInfo(@PathVariable(value = "admin_url") String admin_url,@RequestParam(value = "id") String id, Model model
				) {

			if (checkUrl(admin_url)) {
				model.addAttribute("id", id);
				return "dicinfo";

			} else {
				return "login";
			}
		}
		
	//字典
	@RequestMapping(value = "/system-dic", method = RequestMethod.GET)
	public String systemDic(@PathVariable(value = "admin_url") String admin_url) {
		if (checkUrl(admin_url)) {
			return "system-dic";

		} else {
			return "login";
		}
	}
				
	// 字典内容
	@RequestMapping(value = "/system-dicInfo", method = RequestMethod.GET)
	public String systemDicInfo(@PathVariable(value = "admin_url") String admin_url,@RequestParam(value = "id") String id, Model model
			) {

		if (checkUrl(admin_url)) {
			model.addAttribute("id", id);
			return "system-dicinfo";

		} else {
			return "login";
		}
	}
	
	// 角色
	@RequestMapping(value = "/system-roles", method = RequestMethod.GET)
	public String systemRoles(
			@PathVariable(value = "admin_url") String admin_url
			) {

		if (checkUrl(admin_url)) {
			
			return "system-roles";

		} else {
			return "login";
		}
	}

	// 链接资源
	@RequestMapping(value = "/system-links", method = RequestMethod.GET)
	public String systemLinks(
			@PathVariable(value = "admin_url") String admin_url
			) {

		if (checkUrl(admin_url)) {
			
			return "system-links";

		} else {
			return "login";
		}
	}

	// 权限
	@RequestMapping(value = "/system-authority", method = RequestMethod.GET)
	public String systemAuthority(
			@PathVariable(value = "admin_url") String admin_url
			) {

		if (checkUrl(admin_url)) {
			
			return "system-authority";

		} else {
			return "login";
		}
	}
	
	// 角色权限
	@RequestMapping(value = "/system-roles-authority", method = RequestMethod.GET)
	public String systemRolesAuthority(@PathVariable(value = "admin_url") String admin_url) {

		if (checkUrl(admin_url)) {
			
			return "system-roles-authority";

		} else {
			return "login";
		}
	}
	// 链接权限
	@RequestMapping(value = "/system-links-authority", method = RequestMethod.GET)
	public String systemLinksAuthority(@PathVariable(value = "admin_url") String admin_url) {

		if (checkUrl(admin_url)) {
			
			return "system-links-authority";

		} else {
			return "login";
		}
	}

	// 在线用户管理
	@RequestMapping(value = "/system-online-users", method = RequestMethod.GET)
	public String systemOnlineUsers(
			@PathVariable(value = "admin_url") String admin_url) {

		if (checkUrl(admin_url)) {

			return "system-online-users";

		} else {
			return "login";
		}
	}

	// 在线用户管理
	@RequestMapping(value = "/specialist", method = RequestMethod.GET)
	public String specialist(
			@PathVariable(value = "admin_url") String admin_url) {

		if (checkUrl(admin_url)) {

			return "specialist";

		} else {
			return "login";
		}
	}
	// 支出数据监测
	@RequestMapping(value = "/data-pay", method = RequestMethod.GET)
		public String dataPay(@PathVariable(value = "admin_url") String admin_url) {

			if (checkUrl(admin_url)) {

				return "data-pay";

			} else {
				return "login";
			}
		}
	// 菜单管理
		@RequestMapping(value = "/menu", method = RequestMethod.GET)
			public String menu(@PathVariable(value = "admin_url") String admin_url) {

				if (checkUrl(admin_url)) {

					return "menu";

				} else {
					return "login";
				}
			}
	
}